<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2021/12/25
 * Time: 16:18
 */

namespace app\admin\controller;

use app\admin\model\TransactionDetail;
use app\common\CacheKey;
use think\Db;

class Transaction extends AdminBase
{
    public function getList()
    {
        $data = TransactionDetail::getInstance()->getList();

        $this->assign("data", $data['data']);
        $this->assign("page", $data['page']);
        $this->assign("dinosaur", config('app.dinosaur'));
        $this->assign("keywords", input('param.keywords', ''));
        $this->assign("range", input('param.range', ''));
        $this->assign("dinosaur_type", input('param.dinosaur_type'));

        return $this->fetch();
    }

    /**
     * @Title: export
     * @Describe 导入到excel
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @author lijunwei
     * @Date 2022/1/6 16:12
     */
    public function export()
    {
        $lists = TransactionDetail::getInstance()->exportExcel();
        $result = [];
        foreach ($lists as $key => $list) {
            $result[$key] = [
                'transaction_no' => $list['transaction_no'],
                'return_time' => $list['return_time'],
                'buyers_address' => $list['buyers_address'],
                'seller_address' => $list['seller_address'],
                'dinosaur_name' => $list['dinosaur_name'] . '(' . $list['dinosaur_level_id'] . ')',
                'nft_id' => $list['nft_id'],
                'trans_price' => $list['trans_price'],
                'trans_txid' => $list['trans_txid'],
            ];
        }

        $header = [
            'A' => [
                'title' => '交易号',
                'value' => 'transaction_no',
            ],
            'B' => [
                'title' => '返回时间',
                'value' => 'return_time',
            ],
            'C' => [
                'title' => '买家地址',
                'value' => 'buyers_address',
            ],
            'D' => [
                'title' => '卖家地址',
                'value' => 'seller_address',
            ],
            'E' => [
                'title' => '交易恐龙名称(级别)',
                'value' => 'dinosaur_name',
            ],
            'F' => [
                'title' => '恐龙NFT ID',
                'value' => 'nft_id',
            ],
            'G' => [
                'title' => '成交价(INGold)',
                'value' => 'trans_price',
            ],
            'H' => [
                'title' => '交易txid',
                'value' => 'trans_txid',
            ]
        ];
        $name = '交易明细';

        outExcel($result, $header, $name);
    }
}